/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/test/garbage.c,v 1.3 2002/07/18 18:18:07 bboy Exp $

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "mydnsutil.h"
#include "../mydns/bits.h"
#include "../mydns/header.h"
#include "mydns.h"
#include <netdb.h>

extern char	host[256];											/* Hostname of DNS server */
extern int	port;													/* Port number of DNS server */
extern struct sockaddr_in sa;									/* Address of remote server */
extern int	req;													/* Number of requests */

extern void summary_exit(int);


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	SEND_GARBAGE_REQUEST
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void
send_garbage_request(const unsigned char *buf, size_t len)
{
	static int fd = -1;
	int rv;

	if (fd != -1)
		close(fd);

	/* Connect to server */
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
		Err("%s:%u", host, port);
	if (connect(fd, (const struct sockaddr *)&sa, sizeof(struct sockaddr_in)))
		Err("%s:%u", host, port);

	if ((rv = sendto(fd, buf, len, 0, (struct sockaddr *)&sa, sizeof(sa))) < 0)
		Err("%s:%u: %s", host, port, _("error sending question"));
	usleep(10000);
}
/*--- send_garbage_request() --------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	SEND_GARBAGE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
send_garbage(void)
{
	int ch;
	unsigned char buf[BUFSIZ];
	size_t len;

	for (len = 0; len < DNS_MAXPACKETLEN_UDP; len++)
	{
		printf("sending %d byte packets...\n", len);
		for (ch = 0; ch < 256; ch++)
		{
			memset(buf, ch, len);
			send_garbage_request(buf, len);
			req++;
		}
	}
	summary_exit(0);
}
/*--- send_garbage() ----------------------------------------------------------------------------*/


/* vi:set ts=3: */
/* NEED_PO */
